<template>
  <div>
    <div>
      <el-input
        v-model="row.name"
        placeholder="添加职称..."
        prefix-icon="el-icon-plus"
        size="small"
        class="add-job_title-input"
        @keydown.enter.native="handleCreate"
      />
      <el-select v-model="row.titleLevel" size="small" placeholder="职称等级" class="add-job_title-select">
        <el-option
          v-for="item in titleLevels"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-button type="primary" icon="el-icon-plus" size="small" @click="handleCreate">添加</el-button>
    </div>
    <div class="job_title-table">
      <el-table
        :data="list"
        border
        stripe
        size="small"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="50"
        />
        <el-table-column
          prop="id"
          label="编号"
          width="180"
        />
        <el-table-column
          prop="name"
          label="职称名称"
          width="180"
        />
        <el-table-column
          prop="titleLevel"
          label="职称等级"
          width="180"
        />
        <el-table-column
          prop="createDate"
          label="创建时间"
          width="180"
        />
        <el-table-column
          label="是否启用"
          width="180"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" size="small" type="success">已启用</el-tag>
            <el-tag v-else size="small" type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="danger" size="small" style="margin-top: 10px" :disabled="multipleSelection.length==0" @click="handleBatchDelete">批量删除</el-button>
    </div>
    <el-dialog
      title="修改职称"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :show-close="false"
      width="20%"
    >
      <div>
        <el-tag>职称名称</el-tag>
        <el-input v-model="editRow.name" size="small" class="update-job_title-input" />
        <div class="space" />
        <el-tag>职称等级</el-tag>
        <el-select v-model="editRow.titleLevel" size="small" placeholder="职称等级" class="update-job_title-select">
          <el-option
            v-for="item in titleLevels"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        <div class="space" />
        <el-tag>是否启用</el-tag>
        <el-switch v-model="editRow.enabled" class="update-job_title-switch" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="updateConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { GetAll, Create, Update, Delete, BatchDelete } from '@/api/Sys/SysBasic/JobTitleManage'

export default {
  name: 'JobTitleManage',
  data() {
    return {
      row: {
        name: '',
        titleLevel: ''
      },
      titleLevels: ['正高级', '副高级', '中级', '初级', '员级'],
      list: [],
      dialogVisible: false,
      editRow: {
        name: '',
        titleLevel: '',
        enabled: false
      },
      multipleSelection: []
    }
  },
  mounted() {
    this.getAll()
  },
  methods: {
    getAll() {
      GetAll().then((res) => {
        if (res) {
          this.list = res
        }
      })
    },
    handleCreate() {
      if (this.row.name && this.row.titleLevel) {
        Create(this.row).then((res) => {
          if (res) {
            this.getAll()
            this.row.name = ''
            this.row.titleLevel = ''
          }
        })
      } else {
        this.$message.warning('职称名称和职称等级不可以为空!')
      }
    },
    handleEdit(index, row) {
      Object.assign(this.editRow, row)
      this.dialogVisible = true
    },
    updateConfirm() {
      if (this.editRow.name) {
        Update(this.editRow).then((res) => {
          if (res) {
            this.getAll()
            this.dialogVisible = false
          }
        })
      } else {
        this.$message.warning('职称名称不可以为空!')
      }
    },
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除【' + row.name + '】职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Delete(row.id).then((res) => {
          if (res) {
            this.getAll()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleBatchDelete() {
      this.$confirm('此操作将永久删除【' + this.multipleSelection.length + '】条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var ids = ''
        this.multipleSelection.forEach(element => {
          ids = ids + 'ids=' + element.id + '&'
        })
        BatchDelete(ids).then((res) => {
          if (res) {
            this.getAll()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.add-job_title-input {
  width: 300px;
  margin-right: 10px
}
.add-job_title-select {
  width: 200px;
  margin-right: 10px
}
.job_title-table {
  margin-top: 10px
}
.update-job_title-input {
  width: 250px;
  margin-left: 10px;
}
.update-job_title-select {
  width: 250px;
  margin-left: 10px;
}
.update-job_title-switch {
  width: 250px;
  margin-left: 10px;
}
.space {
  margin-top: 10px;
}
</style>
